package com.kongwg.connction;

import com.mysql.jdbc.Driver;
import com.sun.javaws.jnl.InformationDesc;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author kongwg
 * @create 2021-01-07 11:46
 */
public class ConnctionTest {

    @Test
    public void testConnctions1() throws SQLException {
        Driver driver = new Driver();
        String url = "jdbc:mysql://localhost:3306/test";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "123456");
        Connection conn = driver.connect(url, info);
        System.out.println(conn);

    }

    @Test
    public void test() throws SQLException {
        Driver driver = new Driver();
        String url = "jdbc:mysql://localhost:3306/test";
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");

        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }


    @Test
    public void testConnection2() throws Exception{
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";
        Class driver = Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }


    @Test
    public void testConnectionFlnal() throws IOException, ClassNotFoundException, SQLException {
//        InputStream is = ConnctionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        FileInputStream is = new FileInputStream("src/jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");
        //加载驱动
        Class.forName(driverClass);
        //获取链接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);


    }
}
